// index.js
var api = require("../../api.js"),
  app = getApp();
var r;
Page({
  data: {
    userListInfo: [],
    pageIndex: 2,
    sHeight: '',
    loading: false, //第一次加载，设置true  
    loadingComplete: false, //把“没有数据”设为false，隐藏  
    isFromSearch: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.setData({
      id: options.id,
      type: options.type,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (getApp().globalData.userInfo != null) {
      wx.showLoading({
        title: '数据加载中',
      });
      // 获取评论列表
      var t = new Object();
      var replayarr = [];
      t.pageIndex = "1", t.pageSize = "100", t.otherId = that.data.id, t.ordType = that.data.type;
      wx.request({
        url: api.audio.enrolls,
        data: t,
        success: function(res) {
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              var n = res.data.data;
              if (n.length != 0) {
                var checkStatus = '';
                n.length < 10 ? checkStatus = false : true;
                setTimeout(() => {
                  wx.hideLoading();
                  that.setData({
                    userListInfo: n,
                    loading: checkStatus,
                    loadingComplete: checkStatus,
                    hidden: false
                  });
                }, 500)
              } else {
                setTimeout(() => {
                  wx.hideLoading();
                  that.setData({
                    loadingComplete: false,
                    loading: false,
                    hidden: true
                  })
                }, 500)
              }
            }
          }
        }
      });
    };
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading(), console.log("下拉刷新"), this.loading(),
      setTimeout(function() {
        wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
      }, 1500);
  },


  loading: function() {
    var that = this;
    wx.showNavigationBarLoading();
    var t = new Object();
    t.pageSize = 100, t.pageIndex = that.data.pageIndex, t.otherId = that.data.id, t.ordType = that.data.type;
    wx.request({
      url: api.audio.enrolls,
      data: t,
      success: function(res) {
        if (res.statusCode == 200) {
          if (res.data.code == 200) {
            var n = res.data.data;
            if (n.length > 0) {
              r.data.isFromSearch ? dataList = n : dataList = that.data.userListInfo.concat(n)
              setTimeout(() => {
                that.setData({
                  userListInfo: dataList,
                  loading: true,
                });
              }, 500)
            } else {
              setTimeout(() => {
                that.setData({
                  loadingComplete: true,
                  loading: false,
                })
              }, 500)
            }
          }
        }
      }
    });
  },
  /** 页面上拉触底事件的处理函数  */
  onReachBottom: function() {
    wx.showNavigationBarLoading(), console.log("下拉刷新"), 
    setTimeout(function () {
      let _that = this
      if (_that.data.loading && !_that.data.loadingComplete) {
        _that.setData({
          pageIndex: _that.data.pageIndex + 1,
          isFromSearch: false
        });
        _that.loading();
      };
      wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
    }, 1000);
  },

  /**  页面上拉触底事件的处理函数 */
  onReachBottom: function() {
    let that = this;
    if (that.data.loading && !that.data.loadingComplete) {
      that.setData({
        pageIndex: that.data.pageIndex + 1, //每次触发上拉事件，把searchPageNum+1
        isFromSearch: false //触发到上拉事件，把isFromSearch设为为false
      });
      that.loading();
    };
  },

})